/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package lesson6;

import java.io.BufferedWriter;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.Reader;
import java.io.StreamTokenizer;
import java.io.Writer;
import java.util.Random;
import java.util.Vector;

/**
 *
 * @author netcracker
 */
public class Vectors {

    private static VectorFactory vectorFactory = new ArrayVectorFactory();

    private static void swap(IVector v, int j, int i) {
            double t = v.get(i);
        v.set(v.get(j), i);
        v.set(t, j);
    }
    private int line = 1;
    //private double[] array;

    public static void setVectorFactory(VectorFactory v) {
        vectorFactory = v;
    }

    public static IVector createInstance(int size) {
        return vectorFactory.createVector(size);
    }

    public static IVector createInstance() {
        return vectorFactory.createVector();
    }

    static IVector multiply(IVector v, double f) {
        IVector result = createInstance();
         for (int i = 0; i < v.getLength(); i++) {
         result.add(v.get(i) * f);
         }
         return result;
    }

    static IVector plus(IVector v1, IVector v2) {
        IVector result = createInstance();
        if (v1.getLength() == v2.getLength()) {
            for (int i = 0; i < v1.getLength(); i++) {
                result.add(v1.get(i) + v2.get(i));
            }
        }
        return result;

    }

    public static void sort(IVector v) {
        for (int i = v.getLength() - 1; i > 0; i--) {
         for (int j = 0; j < i; j++) {
         if (v.get(j) > v.get(j + 1)) {
         swap(v, j, j + 1);
         }
         }
         }
    }

    public static IVector randVector(int n) {
        IVector v = createInstance();
        Random rand = new Random();
        for (int i = 0; i < n; i++) {
            v.add(rand.nextDouble());
        }
        return v;
    }

    void outputVector(IVector v, OutputStream out) throws IOException {
        DataOutputStream dos = new DataOutputStream(out);
        dos.writeInt(v.getLength());
        for (int i = 0; i < v.getLength(); i++) {
            dos.writeDouble(v.get(i));
        }
        dos.flush();
    }

    IVector inputVector(InputStream in) throws IOException {
        DataInputStream dis = new DataInputStream(in);
        IVector v = createInstance();
        int b = dis.readInt();
        for (int i = 0; i < b; i++) {
            v.add(dis.readDouble());
        }

        return v;
    }

    void writeVector(IVector v, Writer out) throws IOException {
        BufferedWriter bw = new BufferedWriter(out);
        int length=v.getLength();
        bw.write(""+length);
        for (int i = 0; i < length; i++) {
            bw.write(" "+ v.get(i));
        }
        bw.newLine();
        bw.flush();
    }

    IVector readVector(Reader r) throws IOException {
        IVector v = createInstance();
        double length;
        StreamTokenizer st = new StreamTokenizer(r);
        st.nextToken();
        length = st.nval;
        for (int i = 0; i < length; i++) {
            st.nextToken();
            v.add(st.nval);
        }
        return v;
    }
    
     public static IVector getAdaptedJVector(Vector jvector){
        return (IVector)jvector;
     }
}